package com.bc;

import java.io.File;

import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class BCOptions extends Activity {

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//first display the start screen
		setContentView(R.layout.optionslayout);
	
		//Restore preferences
		SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
	    String username = settings.getString("username", "User");
	    String userstatus = settings.getString("userstatus", "Status");
	    String usercompany = settings.getString("usercompany", "Company");
	    String cardfolder = settings.getString("cardfolder", "/sdcard/");
	    String card = settings.getString("card", "None");
	    String receivedcardfolder = settings.getString("receivedcardfolder", "/sdcard/");

	    //name editText
	    EditText name = (EditText)findViewById(R.id.OptionsEditText01);
	    name.setText(username);
	    
	    //status editText
	    EditText status = (EditText)findViewById(R.id.OptionsEditText02);
	    status.setText(userstatus);
	    
	    //status editText
	    EditText company = (EditText)findViewById(R.id.OptionsEditText05);
	    company.setText(usercompany);
	    
	    //cards folder editText
	    EditText cards = (EditText)findViewById(R.id.OptionsEditText03);
	    cards.setText(cardfolder);
	    
	    //received cards folder editText
	    EditText receivedcards = (EditText)findViewById(R.id.OptionsEditText04);
	    receivedcards.setText(receivedcardfolder);
	    
	    //BROWSE button
	    Button browse = (Button)findViewById(R.id.OptionsBrowseButton);
	    browse.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				EditText cards = (EditText)findViewById(R.id.OptionsEditText03);
				Gallery gallery = (Gallery)findViewById(R.id.Gallery01Option);
				File file = new File(cards.getText().toString());
				if(file.exists())
					gallery.setAdapter(new BCImageAdapter(BCOptions.this,cards.getText().toString()));
				else
					;
				
			}
		});

		//Gallery
	    Gallery gallery = (Gallery)findViewById(R.id.Gallery01Option);
		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

				String [] listefichiers;
				EditText cards = (EditText)findViewById(R.id.OptionsEditText03);
				File repertoire = new File(cards.getText().toString());
				listefichiers=repertoire.list();		

				TextView selected = (TextView)findViewById(R.id.TextView01Option);
				selected.setText(cards.getText().toString()+listefichiers[arg2]);
				
				ImageView card_tn = (ImageView)findViewById(R.id.OptionsCardThumbnail);
				Bitmap myBitmap = BitmapFactory.decodeFile(cards.getText().toString()+listefichiers[arg2]);
				card_tn.setImageBitmap(myBitmap);
			}
		});
		
		//TEXTVIEW : IMAGE SELECTED
		TextView selected = (TextView)findViewById(R.id.TextView01Option);
		selected.setText(card);
	    
		//IMAGEVIEW : IMAGE SELECTED
		ImageView card_tn = (ImageView)findViewById(R.id.OptionsCardThumbnail);
		Bitmap myBitmap = BitmapFactory.decodeFile(card);
		card_tn.setImageBitmap(myBitmap);
		
	    
	    
	    
	    //save button
		Button save = (Button)findViewById(R.id.OptionsButton01);
		save.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
				EditText name2 = (EditText)findViewById(R.id.OptionsEditText01);
				EditText status2 = (EditText)findViewById(R.id.OptionsEditText02);
				EditText company2 = (EditText)findViewById(R.id.OptionsEditText05);
				EditText cards2 = (EditText)findViewById(R.id.OptionsEditText03);
				EditText receivedcards2 = (EditText)findViewById(R.id.OptionsEditText04);
				TextView selected2 = (TextView)findViewById(R.id.TextView01Option);
				
				// We need an Editor object to make preference changes.
			      // All objects are from android.context.Context
			      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(BCOptions.this);
			      SharedPreferences.Editor editor = settings.edit();
			      editor.putString("username", name2.getText().toString());
			      editor.putString("userstatus", status2.getText().toString());
			      editor.putString("usercompany", company2.getText().toString());
			      editor.putString("cardfolder", cards2.getText().toString());
			      editor.putString("receivedcardfolder", receivedcards2.getText().toString());
			      editor.putString("card", selected2.getText().toString());

			      // Commit the edits!
			      editor.commit();
			      
				finish();
			}
			
			
			
			
		});
	    
		
		
		//quit button
		Button cancel = (Button)findViewById(R.id.OptionsButton02);
		cancel.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});
	}
}
